<template>
    <p>vuex页</p>
    <p>count:{{ this.$store.state.count }}</p>
    <p>count:{{ count }}</p> 
    <p>countAlias:{{ countAlias }}</p>
    <p>Getters ===============</p>
    <p>countAddFive:{{ this.$store.getters.countAddFive }}</p>
    <p>countAddFive:{{ countAddFive }}</p>
    <p>countAddFiveAddTen{{ countAddFiveAddTen }}</p>
    <p>countAddFiveAlias:{{ countAddFiveAlias }}</p>
    <p>Mutations ===============</p>
    <button @click="addMut">addMut</button>
    <button @click="addPlayloadMut">addPlayloadMut</button>
    <button @click="add">add</button>
    <button @click="addCommit">addCommit</button>
    <button @click="addAlias">addAlias</button>

    <p>Action=======================</p>
    <button @click="addActionMut">addActionMut</button>
    <button @click="asyncAddActionMut">asyncAddActionMut</button>
    <button @click="asyncAddPlayloadActionMut">asyncAddPlayloadActionMut</button>
    <button @click="addAction">addAction</button>
    <button @click="addActionDispatch">addActionDispatch</button>
    <button @click="addActionAlias">addActionAlias</button>

    <h1>命名空间</h1>
    <p>无命名空间</p>
    <p>访问 模块A的state  {{ $store.state.a.countA }}</p>
    <p>访问 模块A的getters {{ $store.getters.countAA }}</p>
    <p>有命名空间</p>
    <p>访问 模块B的state  {{ $store.state.b.countB }}</p>
    <p>访问 模块B的getters {{ $store.getters['b/countBB']}}</p>


    <h1>Vuex module</h1>
    <button @click="commitIncreaseA">触发-moduleA-mutations</button>
    <button @click="commitAsyncIncreaseA">触发-moduleA-actions</button>
    <button @click="commitIncreaseB">触发-moduleB-mutations</button>
    <button @click="commitAsyncIncreaseB">触发-moduleB-actions</button>

    <h1>MapXXX</h1>
    <h2>全局</h2>
    <p>state</p>
    <p>countA:{{ countA }} </p>
    <p>getters</p>
    <p>countAA:{{ countAA }}</p>
    <p>mutations</p>
    <button @click="increaseA">触发increaseA</button>
    <p>actions</p>
    <button @click="asyncIncreaseA">触发asyncIncreaseA</button>

    <h2>局部</h2>
    <p>moduleB state</p>
    <p>countB::{{ countB }}</p>

    <p>moduleB mutations</p>
    <button @click="increaseB">触发increaseB</button>
     

</template>

<script>
import {mapState, mapGetters,mapMutations,mapActions} from "Vuex"
export default {
    computed:{ 
        // count(){
        //     return this.$store.state.count
        // },
        //test(){
        //     return this.$store.state.test
        // }
        //  数组形式
        // ...mapState(['count','test'])
        // 对象写法
        ...mapState({
        //     count: state => state.count,
        //     countAlias: state => state.count,
        count:'count',
        test:'test',
        countAlias:'count'
         }),
        // countAddFive(){
        //     return this.$store.getters.countAddFive
        // },
        // countAddFiveAddTen(){
        //     return this.$store.getters.countAddFiveAddTen
        // },
        // 数组形式
        // ...mapGetters(['countAddFive','countAddFiveAddTen'])
        //对象写法
        ...mapGetters({
            countAddFive: 'countAddFive',
            countAddFiveAddTen: 'countAddFiveAddTen',
            countAddFiveAlias: 'countAddFive'
        }),
        // 映射模块A的countA
        ...mapState({
            countA: state => state.a.countA
        }),
        // 映射全局getters
        ...mapGetters(['countAA']),

        // 映射模块B的state
        ...mapState('b',['countB'])
   },
    
    methods:{
        addMut(){
            this.$store.commit('add')
        },
        addPlayloadMut(){
            // this.$store.commit('addPlayload',{num:10})
            this.$store.commit({
                type:'addPlayload',
                num:10
            })
        },
        //数组形式
        // ...mapMutations(['add','addPlayload']),
        addCommit(){
            this.addPlayload({num:20})
        },
        ...mapMutations({
            add:'add',
            addAlias:'add'
        }),
        adActionMut(){
            this.$store.dispatch('addAction')
        },
        asyncAddActionMut(){
            this.$store.dispatch('asyncAddAction')
        },
        asyncAddPlayloadActionMut(){
            this.$store.dispatch('asyncAddPlayloadAction',{num:20})
        },
        // ...mapActions(['addAction','asyncAddPloyloadAction']),
        addActionDispatch(){
            this.asyncAddPloyloadAction({num:30})
        },
        ...mapActions({
            addAction:'addAction',
            addActionAlias:'addAction'
        }),
        // 触发模块中的内容
        commitIncreaseA(){
           this.$store.commit('increaseA') 
        },
        commitAsyncIncreaseA(){
            this.$store.dispatch('asyncIncreaseA')
        },
        commitIncreaseB(){
            this.$store.commit('b/increaseB')
        },
        commitAsyncIncreaseB(){
            this.$store.dispatch('b/asyncIncreaseB')
        }
    },
    mounted() {
        console.log(this.$store);
    }
}
</script>